using System.Collections.Generic;
using System.Text.RegularExpressions;
using FireStarter.CodeGeneration.Generator;

namespace FireStarter.CodeGeneration.Parser
{
    public class IfExpressionParser : IActionParser
    {
        private static readonly Regex _regex = new Regex(@"\AIF[(]!*.*\w*[)]\s*:");

        public TemplateExpression Parse(ref string templateText, List<IActionParser> actionParsers)
        {
            int openBracketPos = templateText.IndexOf("(");
            int closeBracketPos = templateText.IndexOf(")");
            openBracketPos++;
            IfExpression ifExpression = new IfExpression(templateText.Substring(openBracketPos, closeBracketPos - openBracketPos));
            int colonPos = templateText.IndexOf(":");
            string statementText = templateText.Substring(colonPos + 1);
            StatementParser statementParser = new StatementParser();
            ifExpression.Part = statementParser.Parse(ref statementText, actionParsers);
            templateText = "";
            return ifExpression;
        }

        public bool IsExpressionMatch(string text)
        {
            return text != null && _regex.IsMatch(text);
        }
    }
}